from django.db import models
from django.contrib.auth.models import User
from comment_utils.moderation import CommentModerator, moderator, AlreadyModerated
from tagging.fields import TagField

 
class Post(models.Model):
  '''
    A blog post model.

    Contains:

      Post.user
      Post.title
      Post.slug
      Post.pub_date
      Post.tags
      Post.enclosure
      Post.body
      Post.enable_comments 
  '''

  user = models.ForeignKey(User)
  title = models.CharField('Title', max_length=200)

  slug = models.SlugField(
    'Slug',
    help_text='Automatically built from the title.'
  )

  pub_date = models.DateTimeField('Publish Date')
  tags = TagField()

  enclosure = models.FileField(
    'Podcast Enclosure',
    upload_to='upload',
    blank=True,
    help_text = 'optional'
  )

  body = models.TextField('Body Text')
  enable_comments = models.BooleanField() 

  def __unicode__(self):
    return self.title

  def get_absolute_url(self):
    return "/b/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)

  def index_url(self):
    return "/b/"

  class Admin:
    list_display = ('user', 'title', 'slug', 'pub_date', 'enable_comments')
    search_fields = ('title', 'description','tags')
    date_hierarchy = 'pub_date'
    # Adding tiny_mce into Django admin:
    # http://www.dehora.net/journal/2006/05/using_tinymce_in_djangos_admin.html
    js = ['tiny_mce/tiny_mce.js', 'js/textareas.js']

  class Meta:
    ordering = ('-pub_date',)


class EntryModerator(CommentModerator):
  akismet = True
  email_notification = True
  enable_field = 'enable_comments'

try:
  moderator.register(Post, EntryModerator)
except AlreadyModerated:
  pass
